package fenetre;

//////////////////////////////////////////IMPORTS
import javax.swing.*;

import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListModel;

import metier.CL_artiste;
import metier.CL_disque;
import metier.Catalogue;

import java.sql.ResultSet;
import java.util.*;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
//import java.awt.List;

public class CL_fenetre extends JFrame implements MouseListener , ActionListener{
	private static final long serialVersionUID = 1L;
	
	//ELEMENTS DE LA FENETRE
	private JTextField tfrecherche;
	private JButton btnNouvelAlbum;
	private JLabel lblHistoriqueDesActions;
	private JButton btnsupprimer;
	private JLabel lblRecherche;
	private JComboBox cbrecherche;
	private JButton btnRechercher;
	private JButton btnToutAfficher;
	private JButton btnModifier;
	private JList list_actions;
	//CLASSES
	private CL_ajt_cd ajoutcd;
	private CL_disque o_disque;
	private CL_artiste o_artiste;
	private Catalogue o_catalogue;
	//VARIABLES
	private ResultSet rs;
	private String[] cd;
	private Object[][] tab = {};
	private String[] title ={};
	private JTable Table_Recherche;
	private DefaultTableModel tableur;
	private int ligne = -1;
	///CONSTRUCTEUR
	public CL_fenetre(){
		build();
		
	}

	//Malema
	/////////////////////////////////////////////////////////
	private void build() {	
		o_artiste = new CL_artiste();
		o_catalogue = new Catalogue();
		
		//		tab = new 
		//JFrame fenetre = new JFrame();
		setSize(634, 396);
		list_actions = new JList();
		btnModifier = new JButton("Modifier");
		btnModifier.addActionListener(this);
		btnModifier.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnNouvelAlbum = new JButton("Nouvel album");
		btnNouvelAlbum.addActionListener(this);
		btnNouvelAlbum.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(this);
		btnRechercher.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnToutAfficher = new JButton("Tout afficher");
		btnToutAfficher.addActionListener(this);
		btnToutAfficher.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		btnsupprimer = new JButton("Supprimer");
		btnsupprimer.addActionListener(this);
		btnsupprimer.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) { }	
			});
		
		tfrecherche = new JTextField();
		tfrecherche.setColumns(10);
		lblRecherche = new JLabel("Recherche");
		cbrecherche = new JComboBox();
		cbrecherche.addItem("Disque");
		cbrecherche.addItem("Auteur");
		cbrecherche.addItem("Genre");

		
		lblHistoriqueDesActions = new JLabel("Historique des actions");
		JScrollPane Jsc_panel = new JScrollPane(Table_Recherche);
		Table_Recherche =  new JTable(tab, title);
		Table_Recherche.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				int colonne;
				Point p = e.getPoint ();
				colonne = Table_Recherche.columnAtPoint (p);
				ligne = Table_Recherche.rowAtPoint (p);
				System.out.println(ligne);
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
	      
	 
		////////////////////////////CONTRAINTES DE PLACEMENT DES ELEMENTS/////
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblRecherche)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(tfrecherche, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(cbrecherche, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnRechercher)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnToutAfficher, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(7)
									.addComponent(Table_Recherche, GroupLayout.PREFERRED_SIZE, 429, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(btnNouvelAlbum, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
								.addComponent(list_actions, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
								.addComponent(lblHistoriqueDesActions)
								.addComponent(btnsupprimer, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
								.addComponent(btnModifier, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
							.addGap(7)))
					.addGap(16))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(16)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblRecherche)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(tfrecherche, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnRechercher)
								.addComponent(cbrecherche, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnToutAfficher)))
						.addComponent(lblHistoriqueDesActions))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(list_actions, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnsupprimer)
							.addGap(10)
							.addComponent(btnModifier)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnNouvelAlbum))
						.addComponent(Table_Recherche, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}

				


//////////////////////////////////////////////////////////////////////////METHODES///////////////////////////////////////
	public void m_method(){
		
	}
////////////////////////////////////////////////////////////////////////////////////ACTION_PERFORMED/////////////////////
	@SuppressWarnings("rawtypes")
	public void actionPerformed(ActionEvent e){
		String nomBouton = e.getActionCommand();

		if (nomBouton == "Nouvel album"){
			CL_ajt_cd ajoutcd = new CL_ajt_cd();
			ajoutcd.setSize(400, 350);
			ajoutcd.setTitle("Ajouter un cd");
			ajoutcd.setDefaultCloseOperation(ajoutcd.DISPOSE_ON_CLOSE);
			ajoutcd.setLocationRelativeTo(null);     
			ajoutcd.setVisible(true);
			ajoutcd.setResizable(false);
		}
		if (nomBouton == "Rechercher"){
			System.out.println("rechercher!");
		}		
		if (nomBouton == "Tout afficher")
		{
			
			System.out.println("afficher tout!");
			o_catalogue.m_afficher_catalogue(0);
			o_disque = new CL_disque();			
			
			tab = o_catalogue.m_afficher_catalogue(0);
			
			
			tableur = new  DefaultTableModel();

			tableur.addColumn("ID");
			tableur.addColumn("Album");
			tableur.addColumn("Date");
			tableur.addColumn("Artiste");
			for(int i = 0 ; i< tab.length; i++)
			{
				tableur.addRow(tab[i]);
			}
			Table_Recherche.setModel(tableur);
			
			
		}
		if (nomBouton == "Supprimer"){
			CL_confirm confirm = new CL_confirm();
			confirm.setSize(300, 110);
			confirm.setTitle("Ajouter un cd");
			confirm.setLocationRelativeTo(null); 
			confirm.setDefaultCloseOperation(confirm.DISPOSE_ON_CLOSE);
			confirm.setVisible(true);
			confirm.setResizable(false);
		}
		
		if (nomBouton == "Modifier"){
			
//			
//			
//			if(ligne != -1)
//			{
//				rs
//				CL_modif_cd omodifcd = new CL_modif_cd();
//				omodifcd.setSize(400, 350);
//				omodifcd.setTitle("Modifier un cd");
//				omodifcd.setDefaultCloseOperation(omodifcd.DISPOSE_ON_CLOSE);
//				omodifcd.setLocationRelativeTo(null);     
//				omodifcd.setVisible(true);
//				omodifcd.setResizable(false);
//			}
		}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
	}
	@Override
	public void mouseExited(MouseEvent arg0) {		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {		
	}
	@Override
	public void mouseClicked(MouseEvent e) {	
		//int cc = e.getClickCount();
		//Object source = e.getSource();
	}
}


